﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CellsLifeActivity
{
    
    class GameLogic
    {
        public static string iniName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6)
                             + "\\cellsSettings.ini";
        public static cIni oIni1 = new cIni(iniName);

        static string sRet;
        static string sRet1;
        static string sRet2;
        static string sRet3;
        static string sRet4;
        static string sRet5;
        static string sRet6;
        static string sRet7;

        public static string SRet
        {
            get
            {
                return sRet;
            }
            set
            {
                sRet = value;
            }
        }

        public static string SRet1
        {
            get
            {
                return sRet1;
            }
            set
            {
                sRet1 = value;
            }
        }

        public static string SRet2
        {
            get
            {
                return sRet2;
            }
            set
            {
                sRet2 = value;
            }
        }

        public static string SRet3
        {
            get
            {
                return sRet3;
            }
            set
            {
                sRet3 = value;
            }
        }

        public static string SRet4
        {
            get
            {
                return sRet4;
            }
            set
            {
                sRet4 = value;
            }
        }

        public static string SRet5
        {
            get
            {
                return sRet5;
            }
            set
            {
                sRet5 = value;
            }
        }

        public static string SRet6
        {
            get
            {
                return sRet6;
            }
            set
            {
                sRet6 = value;
            }
        }

        public static string SRet7
        {
            get
            {
                return sRet7;
            }
            set
            {
                sRet7 = value;
            }
        }

        static MatrixControl mc, mcNew, mcSave;
        static bool globalFlag;
        public static bool GlobalFlag
        {
            get
            {
                return globalFlag;
            }
            set
            {
                globalFlag = value;
            }
        }

        public static void NewGame()
        {

            
            ConsoleKeyInfo cki;

            //size of matrix
            int mRows = 30;
            int mColumns = 30;
            EmptyCell eCell;
            CommonCell cCell;
            PredatoryCell pCell;

            
                //MatrixControl mc, mcNew;
                
                mc = new MatrixControl(mRows, mColumns);
                mcNew = new MatrixControl(mRows, mColumns);
                eCell = new EmptyCell();
                cCell = new CommonCell();
                pCell = new PredatoryCell();

                
                //string iniName = "E:\\cellsSettings.ini";
                //cIni oIni1 = new cIni(iniName);
              //  GameLogic.SRet = oIni1.ReadValue("Cells percentage", "CommonCellsPercent", "");
             //   GameLogic.SRet1 = oIni1.ReadValue("Cells percentage", "PredatoryCellsPercent", "");
                GameLogic.SRet2 = oIni1.ReadValue("Empty Cell", "EnoughNumberOfCommonNeighbours", "");
                GameLogic.SRet3 = oIni1.ReadValue("Empty Cell", "EnoughNumberOfPredatoryNeighbours", "");
                GameLogic.SRet4 = oIni1.ReadValue("Common Cell", "NumberOfEmptyNeighbours", "");
                GameLogic.SRet5 = oIni1.ReadValue("Common Cell", "NumberOfSameKindNeighbours", "");
                GameLogic.SRet6 = oIni1.ReadValue("Predatory Cell", "NumberOfEmptyNeighbours", "");
                GameLogic.SRet7 = oIni1.ReadValue("Predatory Cell", "NumberOfSameKindNeighbours", "");


                if (globalFlag == true)
                {
                    Console.Clear();
                    mc.PrintMatrix();
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("Press <Spasebar> to simulate new state");
                    Console.WriteLine("Press <M> to return to Main Menu");
                    Console.WriteLine("Press <Escape> to exit");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                    for (int i = 0; i < mColumns; i++)
                        for (int j = 0; j < mRows; j++)
                        {
                            mc.MatrixCell[i, j] = (Cell)(mcSave.MatrixCell[i, j]).Clone();
                        }
                    
                //Console.ReadLine();

            do
            {
                cki = Console.ReadKey();

                switch (cki.Key)
                {
                    case ConsoleKey.Spacebar:

                        for (int i = 0; i < mRows; i++)
                            for (int j = 0; j < mColumns; j++)
                            {
                                int count = 0;

                                if ((i > 0) && (j > 0))
                                    if (mc.MatrixCell[i - 1, j - 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;
                                if (i > 0)
                                    if (mc.MatrixCell[i - 1, j].GetType() == mc.MatrixCell[i, j].GetType()) count++;
                                if ((i > 0) && (j < mRows - 1))
                                    if (mc.MatrixCell[i - 1, j + 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;

                                if (j > 0)
                                    if (mc.MatrixCell[i, j - 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;
                                if (j < mc.MatrixCell.GetLength(1) - 1)
                                    if (mc.MatrixCell[i, j + 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;

                                if ((i < mc.MatrixCell.GetLength(0) - 1) && (j > 0))
                                    if (mc.MatrixCell[i + 1, j - 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;
                                if (i < mc.MatrixCell.GetLength(0) - 1)
                                    if (mc.MatrixCell[i + 1, j].GetType() == mc.MatrixCell[i, j].GetType()) count++;
                                if ((i < mc.MatrixCell.GetLength(0) - 1) && (j < mc.MatrixCell.GetLength(1) - 1))
                                    if (mc.MatrixCell[i + 1, j + 1].GetType() == mc.MatrixCell[i, j].GetType()) count++;

                                //cell dying from solitude
                                if (mc.MatrixCell[i, j].GetType() != eCell.GetType())
                                    mcNew.MatrixCell[i, j] = (Cell)(mc.MatrixCell[i, j].DieAlone(count, mcNew.MatrixCell[i, j])).Clone();
                                //mcNew.MatrixCell[i, j] = mc.MatrixCell[i, j].Die(count, mcNew.MatrixCell[i, j]);

                                int countC = 0;  //count CommonCell
                                int countP = 0;  //count PredatoryCell
                                int countE = 0;  //count EmptyCell
                                int iEat = 0;
                                int jEat = 0;
                                bool flag = false;

                                if ((i > 0) && (j > 0))
                                {
                                    if (mc.MatrixCell[i - 1, j - 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i - 1, j - 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i - 1, j - 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i - 1;
                                        jEat = j - 1;
                                        flag = true;
                                    }
                                }
                                if (i > 0)
                                {
                                    if (mc.MatrixCell[i - 1, j].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i - 1, j].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i - 1, j].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i - 1;
                                        jEat = j;
                                        flag = true;
                                    }
                                }
                                if ((i > 0) && (j < mRows - 1))
                                {
                                    if (mc.MatrixCell[i - 1, j + 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i - 1, j + 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i - 1, j + 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i - 1;
                                        jEat = j + 1;
                                        flag = true;
                                    }
                                }

                                if (j > 0)
                                {
                                    if (mc.MatrixCell[i, j - 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i, j - 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i, j - 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i;
                                        jEat = j - 1;
                                        flag = true;
                                    }
                                }
                                if (j < mc.MatrixCell.GetLength(1) - 1)
                                {
                                    if (mc.MatrixCell[i, j + 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i, j + 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i, j + 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i;
                                        jEat = j + 1;
                                        flag = true;
                                    }
                                }

                                if ((i < mc.MatrixCell.GetLength(0) - 1) && (j > 0))
                                {
                                    if (mc.MatrixCell[i + 1, j - 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i + 1, j - 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i + 1, j - 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i + 1;
                                        jEat = j - 1;
                                        flag = true;
                                    }
                                }
                                if (i < mc.MatrixCell.GetLength(0) - 1)
                                {
                                    if (mc.MatrixCell[i + 1, j].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i + 1, j].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i + 1, j].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i + 1;
                                        jEat = j;
                                        flag = true;
                                    }
                                }
                                if ((i < mc.MatrixCell.GetLength(0) - 1) && (j < mc.MatrixCell.GetLength(1) - 1))
                                {
                                    if (mc.MatrixCell[i + 1, j + 1].GetType() == cCell.GetType()) countC++;
                                    if (mc.MatrixCell[i + 1, j + 1].GetType() == pCell.GetType()) countP++;
                                    if (mc.MatrixCell[i + 1, j + 1].GetType() == eCell.GetType()) countE++;
                                    if ((countC == 1) && (flag == false))
                                    {
                                        iEat = i + 1;
                                        jEat = j + 1;
                                        flag = true;
                                    }
                                }

                                //borning of new cell
                                if (mc.MatrixCell[i, j].GetType() == eCell.GetType())
                                    mcNew.MatrixCell[i, j] = (Cell)(mc.MatrixCell[i, j].MadeNewCell(countC, countP,
                                                                                        mcNew.MatrixCell[i, j])).Clone();

                                //eating
                                if (mc.MatrixCell[i, j].GetType() == pCell.GetType())
                                    mcNew.MatrixCell[iEat, jEat] = (Cell)(mc.MatrixCell[iEat, jEat].EatNeighbour(countC,
                                                                                        mcNew.MatrixCell[iEat, jEat])).Clone();

                                //cell dying from overpopulation
                                if (mc.MatrixCell[i, j].GetType() != eCell.GetType())
                                    mcNew.MatrixCell[i, j] = (Cell)(mc.MatrixCell[i, j].DieOverpopulation(countE,
                                                                                        mcNew.MatrixCell[i, j])).Clone();

                                flag = false;
                            }

                
                        Console.Clear();
                        mcNew.PrintMatrix();
                        Console.WriteLine("");
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.WriteLine("Press <Spasebar> to simulate new state");
                        Console.WriteLine("Press <M> to return to Main Menu");
                        Console.WriteLine("Press <Escape> to exit");
                        Console.ForegroundColor = ConsoleColor.White;
                        for (int i = 0; i < mColumns; i++)
                            for (int j = 0; j < mRows; j++)
                            {
                                mc.MatrixCell[i, j] = (Cell)(mcNew.MatrixCell[i, j]).Clone();
                                //mc.MatrixCell[i, j] = mcNew.MatrixCell[i, j];
                            }

                      //  Console.WriteLine(GameLogic.SRet + " " + GameLogic.SRet1 + " " + GameLogic.SRet2 + " " + GameLogic.SRet3 + " "
                        //    + GameLogic.SRet4 + " " + GameLogic.SRet5 + " " + GameLogic.SRet6 + " " + GameLogic.SRet7);
                        break; 
                    case ConsoleKey.M:
                        Program.PrintMainMenu();
                        break;
                    default:
                        //Console.Write(" ");
                        break;
                }

            } while (cki.Key != ConsoleKey.Escape);
        }


        public static void Game()
        {
            //size of matrix
            int mRows = 30;
            int mColumns = 30;
            mcSave = new MatrixControl(30,30);
                        
            Console.Clear();
            mcNew.PrintMatrix();
            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("Press <Spasebar> to simulate new state");
            Console.WriteLine("Press <M> to return to Main Menu");
            Console.WriteLine("Press <Escape> to exit");
            Console.ForegroundColor = ConsoleColor.White;
            for (int i = 0; i < mColumns; i++)
                for (int j = 0; j < mRows; j++)
                {
                    mc.MatrixCell[i, j] = (Cell)(mcNew.MatrixCell[i, j]).Clone();
                    mcSave.MatrixCell[i, j] = (Cell)(mcNew.MatrixCell[i, j]).Clone();
                    //mc.MatrixCell[i, j] = mcNew.MatrixCell[i, j];
                }

           // string iniName = "E:\\cellsSettings.ini";
           // cIni oIni1 = new cIni(iniName);
            GameLogic.SRet = oIni1.ReadValue("Cells percentage", "CommonCellsPercent", "");
            GameLogic.SRet1 = oIni1.ReadValue("Cells percentage", "PredatoryCellsPercent", "");
            GameLogic.SRet2 = oIni1.ReadValue("Empty Cell", "EnoughNumberOfCommonNeighbours", "");
            GameLogic.SRet3 = oIni1.ReadValue("Empty Cell", "EnoughNumberOfPredatoryNeighbours", "");
            GameLogic.SRet4 = oIni1.ReadValue("Common Cell", "NumberOfEmptyNeighbours", "");
            GameLogic.SRet5 = oIni1.ReadValue("Common Cell", "NumberOfSameKindNeighbours", "");
            GameLogic.SRet6 = oIni1.ReadValue("Predatory Cell", "NumberOfEmptyNeighbours", "");
            GameLogic.SRet7 = oIni1.ReadValue("Predatory Cell", "NumberOfSameKindNeighbours", "");
        }

    }
}
